package com.easybuy.daoimpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.codec.digest.DigestUtils;

import com.easybuy.dao.BaseDao;
import com.easybuy.dao.UserDao;
import com.easybuy.entily.User;


public class UserDaoImpl extends BaseDao implements UserDao{

	@Override
	public int addUser(User use) {//ע��
		// TODO Auto-generated method stub
	    String sql="insert into easybuy_user(loginName,userName,password,sex,identityCode,email,mobile,type) values(?,?,?,?,?,?,?,?)";
		Object [] obj={use.getLoginName(),use.getUserName(),use.getPassword(),use.getSex(),use.getIdentityCode(),use.getEmail(),use.getMobile(),use.getType()};
		return super.executeUpdate(sql, obj);
	}
	@Override
	public int updateNewsInfo(User u) {//�޸�
		// TODO Auto-generated method stub
		String sql="UPDATE easybuy_user SET loginName=?,userName=?,password=?,sex=?,identityCode=?,email=?,mobile=?,type=? WHERE id=?";
		Object []  obj={u.getLoginName(),u.getUserName(),u.getPassword(),u.getSex(),u.getIdentityCode(),u.getEmail(),u.getMobile(),u.getType(),u.getId()};
		return super.executeUpdate(sql, obj);
	}
	@Override
	public int delNewsInfo(int id) {//ɾ��
		// TODO Auto-generated method stub
		
		String sql="DELETE FROM easybuy_user WHERE id=?";
		Object []  obj={id};
		return super.executeUpdate(sql, obj);
	}
	@Override
	public User UserByLoginName(String loginname,String password) {//�Լ����ĵ�½
		// TODO Auto-generated method stub
		Connection conn = getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;
		User bpu = null;
		try {
			String sql = "select * from easybuy_user where loginName=? and password=?";
			ps = conn.prepareStatement(sql);
			ps.setString(1,loginname);
			ps.setString(2, password);
			rs = ps.executeQuery();
			while(rs.next()){
				bpu = new User();
				bpu.setId(rs.getInt("id"));
				bpu.setLoginName(rs.getString("loginName"));
				bpu.setUserName(rs.getString("userName"));
				bpu.setPassword(rs.getString("password"));
				bpu.setSex(rs.getInt("sex"));
				bpu.setIdentityCode(rs.getString("identityCode"));
				bpu.setEmail(rs.getString("email"));
				bpu.setMobile(rs.getString("mobile"));
				bpu.setType(rs.getInt("type"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			this.closeAll(conn, ps, rs);
		}
		return bpu;
	}
	@Override
	public List<User> SelectUser() {//�������û�
		// TODO Auto-generated method stub
		Connection conn = getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;
		List<User> list = new ArrayList<User>();
		User bpu = null;
		try {
			String sql = "select * from easybuy_user";
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while(rs.next()){
				bpu = new User();
				bpu.setId(rs.getInt("id"));
				bpu.setLoginName(rs.getString("loginName"));
				bpu.setUserName(rs.getString("userName"));
				bpu.setPassword(rs.getString("password"));
				bpu.setSex(rs.getInt("sex"));
				bpu.setIdentityCode(rs.getString("identityCode"));
				bpu.setEmail(rs.getString("email"));
				bpu.setMobile(rs.getString("mobile"));
				bpu.setType(rs.getInt("type"));
				list.add(bpu);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			this.closeAll(conn, ps, rs);
		}
		return list;
	}

}
